package src.src;

/**
 * Clase de implementación de MergeSort
 * Adaptada del libro "Data Structures in Java for the Principled Programmer"
 * @author Duane A. Bailey - Grupo 5
 */
public class mergeSort {

    /**
     * Retiene temporalmente los datos
     */
    protected static Comparable[] data;

    /**
     * Realiza un ordenamiento con el algoritmo de intercalado
     * pre: 0 <= n <= data.length
     * post: valores en data[0..n-1] en orden ascendente
     * @param list datos a ordenar
     * @param lo límite inferior
     * @param n número de datos a ordenar
     */
    public static void mergeSort_srt(Comparable[] list, int lo, int n) {
        int low = lo;
        int high = n;
        if (low >= high) {
            return;
        }
        int middle = (low + high) / 2;
        mergeSort_srt(list, low, middle);
        mergeSort_srt(list, middle + 1, high);

        int end_low = middle;
        int start_high = middle + 1;

        while ((lo <= end_low) && (start_high <= high)) {
            if (list[low].compareTo(list[start_high]) < 0) {
                low++;
            } else {
                Comparable Temp = list[start_high];
                for (int k = start_high - 1; k >= low; k--) {
                    list[k + 1] = list[k];
                }
                list[low] = Temp;
                low++;
                end_low++;
                start_high++;
            }
        }

        setData(list);
    }

    /**
     * 
     * @return
     */
    public static Comparable[] getData() {
        return data;
    }

    /**
     * 
     * @param data
     */
    public static void setData(Comparable[] data) {
        mergeSort.data = data;
    }
}
